﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Nop.Web.Models.Customer;
using Nop.Web.Framework.UI.Captcha;
using Nop.Services.Customers;
using Nop.Services.Authentication;
using Nop.Services.Localization;
using Nop.Services.Topics;
using Nop.Web.Models.Topics;
using Telerik.Web.Mvc;
using Nop.Core.Domain.Topics;

namespace Nop.Web.Controllers
{
    public class HotelController : Controller
    {
        //
        // GET: /Hotel/
        private readonly ICustomerRegistrationService _customerRegistrationService;
        private readonly IAuthenticationService _authenticationService;
        private readonly ICustomerService _customerService;
        private readonly ILocalizationService _localizationService;
        private readonly ITopicService _topicService;

        public HotelController(ICustomerRegistrationService customerRegistrationService,
                               IAuthenticationService authenticationService,
                               ICustomerService customerService,
                               ILocalizationService localizationService,
                               ITopicService topicService)
        {
            _customerRegistrationService = customerRegistrationService;
            _authenticationService = authenticationService;
            _customerService = customerService;
            _localizationService = localizationService;
            _topicService = topicService;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Login()
        {
            var model = new LoginModel();
            model.UsernamesEnabled = true;
            return View(model);
        }

        [HttpPost]
        [CaptchaValidator]
        public ActionResult Login(LoginModel model, string returnUrl, bool captchaValid)
        {
            if (_customerRegistrationService.ValidateCustomer(model.Email, model.Password))
            {
                var customer = _customerService.GetCustomerByEmail(model.Email);

                //sign in new customer
                _authenticationService.SignIn(customer, model.RememberMe);

                return Redirect("~/admin");
            }
            else
            {
                ModelState.AddModelError("", _localizationService.GetResource("Account.Login.WrongCredentials"));
            }
            
            //If we got this far, something failed, redisplay form
            model.UsernamesEnabled = false;
            model.DisplayCaptcha =true;
            return View(model);
        }

        public ActionResult ListTopic()
        {
            var topics = _topicService.GetAllTopics();
            var gridModel = new GridModel<TopicModel>
            {
                Data = topics.Select(x => ToModel(x)),
                Total = topics.Count
            };
            return View(gridModel);
        }

        public ActionResult ViewTopic(int topicID)
        {
            var topic = _topicService.GetTopicById(topicID);
            return View(ToModel(topic));
        }

        private TopicModel ToModel(Topic entity)
        {
            TopicModel tm = new TopicModel() 
            {
                Title = entity.Title,
                Body = entity.Body,
                Id = entity.Id,
                SystemName = entity.SystemName
            };
            return tm;
        }
    }
}
